package com.atguigu.gmall.search.config;

import com.rabbitmq.client.ConfirmCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.annotation.PostConstruct;

@Slf4j
@Configuration
public class MqConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        // b 消息是否成功收到 p->exchange msg:错误消息
        rabbitTemplate.setConfirmCallback((correlationData, b,   msg)->{
            if (!b){
                log.error(correlationData.getId()+msg);
            }
        });

        /**
         *
         * @param message 投递失败的消息
         * @param code 回复的状态码
         * @param txt 回复的文本内容
         * @param x 当时这个消息发送给那个交换机
         * @param router 当时这个消息用那个路由建
         */
        //交换机发送给队列失败回调
        rabbitTemplate.setReturnCallback((message,  code,  txt,  x,  router)->{
            log.error("消息:{},状态码:{},回复内容:{},交换机:{},路由键:{}",message,code,txt,x,router);
        });
    }
}
